package com.ruoyi.web.controller.project;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.StoreInventory;
import com.ruoyi.system.service.IStoreInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/project/storeInventory")
public class StoreInventoryController {
    @Autowired
    private IStoreInventoryService storeInventoryService;

    @GetMapping("/{storeId}/{itemId}")
    public AjaxResult getInventory(@PathVariable("storeId") String storeId, @PathVariable("itemId") String itemId) {
        StoreInventory inventory = storeInventoryService.selectInventoryByStoreAndItem(storeId, itemId);
        return AjaxResult.success(inventory);
    }

    @GetMapping("/list")
    public AjaxResult listInventories(StoreInventory storeInventory) {
        List<StoreInventory> list = storeInventoryService.selectInventoryList(storeInventory);
        return AjaxResult.success(list);
    }

    @PostMapping("/add")
    public AjaxResult addInventory(@RequestBody StoreInventory storeInventory) {
        int result = storeInventoryService.insertInventory(storeInventory);
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @PostMapping("/update")
    public AjaxResult updateInventory(@RequestBody StoreInventory storeInventory) {
        int result = storeInventoryService.updateInventory(storeInventory);
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @DeleteMapping("/{storeId}/{itemId}")
    public AjaxResult deleteInventory(@PathVariable String storeId, @PathVariable String itemId) {
        int result = storeInventoryService.deleteInventoryByStoreAndItem(storeId, itemId);
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @DeleteMapping("/batch")
    public AjaxResult deleteInventories(@RequestBody String storeId, @RequestBody String[] itemIds) {
        int result = storeInventoryService.deleteInventoryByStoreAndItems(storeId, itemIds);
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @PostMapping("/transfer")
    public AjaxResult transferInventory(@RequestParam String sourceStoreId,
                                        @RequestParam String targetStoreId,
                                        @RequestParam String itemId,
                                        @RequestParam int quantity) {
        boolean success = storeInventoryService.transferInventory(sourceStoreId, targetStoreId, itemId, quantity);
        return success ? AjaxResult.success() : AjaxResult.error("Transfer failed");
    }
} 